/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.platform.web.controller.agent;

import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.util.PageUtils;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.platform.model.dto.OrderOptionDto;
import com.medusa.gruul.platform.model.dto.agent.AgenInfoDto;
import com.medusa.gruul.platform.model.dto.agent.BatchOptionDto;
import com.medusa.gruul.platform.model.dto.agent.WithdrawOrderAuditDto;
import com.medusa.gruul.platform.model.vo.agent.*;
import com.medusa.gruul.platform.service.IAgentInfoService;
import com.medusa.gruul.platform.service.IAgentPackageOrderService;
import com.medusa.gruul.platform.service.IAgentRechargeService;
import com.medusa.gruul.platform.service.IAgentWithdrawOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author whh
 * @description
 * @data: 2020/10/9
 */
@RestController
@RequestMapping("/agent/admin")
@Api(tags = "代理后台相关")
public class AgentInfoAdminController {

    @Autowired
    private IAgentInfoService infoService;
    @Autowired
    private IAgentWithdrawOrderService agentWithdrawOrderService;
    @Autowired
    private IAgentPackageOrderService agentPackageOrderService;
    @Autowired
    private IAgentRechargeService agentRechargeService;


    @PutMapping("/motify/info")
    @EscapeLogin
    @ApiOperation(value = "新增或修改代理信息接口", tags = "代理管理台相关")
    public Result motifyInfo(@RequestBody AgenInfoDto dto) {
        infoService.motifyInfo(dto);
        return Result.ok();
    }


    @GetMapping("/info/{agentId}")
    @EscapeLogin
    @ApiOperation(value = "获取指定代理详细信息", tags = "代理管理台相关")
    public Result<AgentDetailInfo> getInfo(@PathVariable Long agentId) {
        AgentDetailInfo info = infoService.getByAgentId(agentId);
        return Result.ok(info);
    }


    @GetMapping("/infos")
    @EscapeLogin
    @ApiOperation(value = "获取代理商或直属渠道商列表", tags = "代理管理台相关")
    public Result<PageUtils<AgentInfosVo>> agentInfos(@ApiParam(value = "指定页数", required = true) @RequestParam Integer page,
                                                      @ApiParam(value = "数据条数", required = true) @RequestParam Integer size,
                                                      @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
                                                      @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
                                                      @ApiParam(value = "名称或电话") @RequestParam(required = false) String search,
                                                      @ApiParam(value = "父级代理id") @RequestParam(required = false) Long parentId,
                                                      @ApiParam(value = "搜索类型 type=1:代理商 2:直属渠道商 3:间接渠道商 ") @Range(min = 1, max = 3, message = "错误类型") @RequestParam Integer type
    ) {
        PageUtils<AgentInfosVo> pageVos = infoService.agentInfos(page, size, startTime, endTime, search, type, parentId);
        return Result.ok(pageVos);
    }

    @PutMapping("/batch/option")
    @EscapeLogin
    @ApiOperation(value = "批量操作", tags = "代理管理台相关")
    public Result batchOption(@RequestBody BatchOptionDto discountDto) {
        infoService.batchOption(discountDto);
        return Result.ok();
    }

    @GetMapping("/withdraw/order")
    @EscapeLogin
    @ApiOperation(value = "管理台查询所有代理提现工单", tags = "代理管理台相关")
    public Result<AdminWithdrawOrdersVo> adminWithdrawOrders(@ApiParam(value = "指定页数", required = true) @RequestParam Integer page,
                                                             @ApiParam(value = "数据条数", required = true) @RequestParam Integer size,
                                                             @ApiParam(value = "工单状态 0:待审核 1:审核拒绝 2:已打款") @Valid @NotNull @Range(min = 0, max = 1) @RequestParam Integer status,
                                                             @ApiParam(value = "提现方式： 1-微信  2-支付宝  3-银行卡") @RequestParam(required = false) Integer type,
                                                             @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
                                                             @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
                                                             @ApiParam(value = "订单号或账号") @RequestParam(required = false) String search,
                                                             @ApiParam(value = "代理级别 1代理商2直属渠道商3间接渠道商") @RequestParam(required = false) Integer agentType
    ) {
        AdminWithdrawOrdersVo vo = agentWithdrawOrderService.adminWithdrawOrders(page, size, status, type, startTime, endTime, search, agentType);
        return Result.ok(vo);
    }


    @PutMapping("/withdraw/order/audit")
    @EscapeLogin
    @ApiOperation(value = "提现工单审核", tags = "代理管理台相关")
    public Result withdrawOrderAudit(@RequestBody @Validated WithdrawOrderAuditDto withdrawOrderAuditDto) {
        agentWithdrawOrderService.withdrawOrderAudit(withdrawOrderAuditDto);
        return Result.ok();
    }


    @GetMapping("/recharge/order")
    @EscapeLogin
    @ApiOperation(value = "管理台分页查询代理的充值订单", tags = "代理后台相关")
    public Result<PageUtils<BalanceDetailsVo>> balanceDetails(@ApiParam(value = "指定页数", required = true) @RequestParam Integer page,
                                                              @ApiParam(value = "数据条数", required = true) @RequestParam Integer size,
                                                              @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
                                                              @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
                                                              @ApiParam(value = "支付方式 0-所有 1支付宝 2 微信 3汇款支付 4-代理余额支付") @RequestParam(required = false, defaultValue = "0") Integer payType,
                                                              @ApiParam(value = "订单状态 1:充值中 2:充值成功 3:充值成功") @Valid @Max(value = 3) @Min(value = 1)
                                                              @RequestParam Integer status,
                                                              @ApiParam(value = "手机号") @RequestParam(required = false) String phone,
                                                              @ApiParam(value = "充值订单号") @RequestParam(required = false) String rechargeNum,
                                                              @ApiParam(value = "商户昵称") @RequestParam(required = false) String nikeName
    ) {
        PageUtils<BalanceDetailsVo> result = agentRechargeService.payOrder(page, size, startTime, endTime, payType, status, phone, rechargeNum, nikeName);
        return Result.ok(result);
    }

    @PutMapping("/recharge/order/option")
    @EscapeLogin
    @ApiOperation(value = "管理台代理的充值订单操作")
    public Result orderOption(@RequestBody OrderOptionDto dto) {
        agentRechargeService.orderOption(dto);
        return Result.ok();
    }

    @GetMapping("/package/order")
    @EscapeLogin
    @ApiOperation(value = "管理台分页查询代理的订购订单")
    public Result<PageUtils<AgentPackageOrdersVo>> agentOrders(
            @ApiParam(value = "指定页数", required = true) @RequestParam Integer page,
            @ApiParam(value = "数据条数", required = true) @RequestParam Integer size,
            @ApiParam(value = "支付方式 0-所有 1:余额支付2:微信3:支付宝 4:汇款支付 ") @RequestParam(required = false, defaultValue = "0") Integer payType,
            @ApiParam(value = "充值状态 0-所有 1:处理中2:已经完成 3:关闭") @RequestParam(required = false, defaultValue = "0") Integer status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String payStartTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String payEndTime,
            @ApiParam(value = "手机号") @RequestParam(required = false) String phone,
            @ApiParam(value = "订单号") @RequestParam(required = false) String orderNum,
            @ApiParam(value = "订购模板") @RequestParam(required = false) Long templateId,
            @ApiParam(value = "商户昵称") @RequestParam(required = false) String nikeName
    ) {
        PageUtils<AgentPackageOrdersVo> record = agentPackageOrderService.agentOrders(page, size, status, payStartTime, payEndTime, phone, orderNum, nikeName, payType, templateId);
        return Result.ok(record);
    }

    @PutMapping("/package/order/option")
    @EscapeLogin
    @ApiOperation(value = "管理台代理的订购订单操作")
    public Result packageOrderOption(@RequestBody OrderOptionDto dto) {
        agentPackageOrderService.packageOrderOption(dto);
        return Result.ok();
    }

}
